package com.leetcode.algorithm.y22.m08.w2;

/**
 * 11. 盛最多水的容器
 * 
 * https://leetcode.cn/problems/container-with-most-water/
 * 
 * @author jie.deng
 *
 */
class Question0011Solution01 {

	public int maxArea(int[] height) {
		// 双指针
		int left = 0;
		int right = height.length - 1;
		int area = (right - left) * Math.min(height[left], height[right]);
		int maxArea = area;
		while (left < right) {
			if (height[left] < height[right]) {
				int l = height[left];
				do {
					left++;
				} while (height[left] < l);
				area = (right - left) * Math.min(height[left], height[right]);
				maxArea = Math.max(maxArea, area);
			} else {
				int r = height[right];
				do {
					right--;
				} while (height[right] < r);
				area = (right - left) * Math.min(height[left], height[right]);
				maxArea = Math.max(maxArea, area);
			}
		}
		return maxArea;
	}

}